package Logic

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"server/Project_User"
	"server/model"
	"server/response"
	"server/servers"
	"time"
)

// InitResourceByUser 创建角色时拿到玩家id，为他的各项资源信息初始化
func InitResourceByUser(ctx *gin.Context, userid int) (err error) {
	var resource model.ResourceP
	resource.UserId = userid
	resource.UpdateTime = time.Now()
	var Type = [6]string{"stone", "iron", "gold", "diamond", "goldCoin", "silverCoin"}
	for i := range Type {
		resource.Type = Type[i]
		err = servers.Svc.ResourceModel.Insert(resource)
		err = servers.Svc.ResourceModel.UpdateCount(userid, "1", Type[i])
		err = servers.Svc.ResourceModel.UpdateMaxCount(userid, "100", Type[i])
		err = servers.Svc.ResourceModel.UpdateAddRate(userid, "1", Type[i])
		if err != nil {
			response.Fail(ctx, nil, "初始化资源信息出现问题")
			return err
		}
	}
	return nil
}

// GetResourceInfo 实现查找玩家信息
func GetResourceInfo(ctx *gin.Context, userid int) ([]model.ResourceP, error) {
	resource, err := servers.Svc.ResourceModel.SelectById(userid)
	if err != nil {
		response.Fail(ctx, nil, "查找玩家资源出现问题")
		return nil, err
	}
	return resource, nil

}

func UpdateResourceCount(ctx *gin.Context, userid int, resource Project_User.UpdateResource) error {
	var resourceMap = map[string]string{
		"diamond":    resource.Diamond,
		"gold":       resource.Gold,
		"stone":      resource.Stone,
		"iron":       resource.Iron,
		"goldCoin":   resource.GoldCoin,
		"silverCoin": resource.SilverCoin,
	}
	for i := range resourceMap {
		err := servers.Svc.ResourceModel.UpdateCount(userid, resourceMap[i], i)
		if err != nil {
			fmt.Println("资源更新出现问题")
			response.Fail(ctx, nil, "资源更新时出现问题")
			return err
		}
	}
	return nil
}
